//
// Created by Shith on 2016/10/4.
//

#include "sdk/Commands.h"
#include "json/json.h"
#include "sdk/misc.h"

Commands Commands::merge(const Commands& cmd1, const Commands& cmd2)
{
	Commands cmd;
    cmd.cmds.insert(cmd.cmds.end(), cmd1.cmds.begin(), cmd1.cmds.end());
	cmd.cmds.insert(cmd.cmds.end(), cmd2.cmds.begin(), cmd2.cmds.end());
    return cmd;
}//合并两个PCommand

std::string Commands::to_string() {
    Json::Value root;
    for (auto i: cmds){
        Json::Value x;
        x["name"] = i.name;
        for (auto j: i.params)
            x["params"].append(j);
        root.append(x);
    }
    return jsonToString(root);
}

Commands Commands::from_string(const std::string str) {
    Json::Value root = stringToJson(str);
    Commands pCommand;
    for (int i = 0; i < root.size(); ++ i){
        Command command;
        command.name = root[i]["name"].asString();
        for (int j = 0; j < root[i]["params"].size(); ++ j)
            command.params.push_back(root[i]["params"][j].asString());
        pCommand.cmds.push_back(command);
    }
    return pCommand;
}

void Commands::clear() {
    cmds.clear();
}

void Commands::merge(const Commands &cmd) {
    Commands tmp = Commands::merge(*this, cmd);
    (*this) = tmp;
}
